import { lazy } from 'react'
import { MetaMenu, AuthRouteObject } from './interface'
import { MENU_LIST } from '@/config/menu'
import { Navigate } from 'react-router'

// 快速导入工具函数
const lazyLoad = (moduleName: string) =>
  lazy(() => import(`@/pages/${moduleName}/index.tsx`))

const Login = lazyLoad('login')
const Home = lazyLoad('home')

const MENU_ROUTERS: AuthRouteObject<MetaMenu>[] = MENU_LIST.map((item) => {
  const Element = lazyLoad(item.menuKey)
  return {
    path: '/' + item.menuKey,
    element: <Element />,
    meta: {
      title: item.name,
    }
  }
})
const RedirectToLogin = () => (
  <Navigate
    to="/login"
    replace
  />
)
const BASE_ROUTERS: AuthRouteObject<MetaMenu>[] = [
  {
    path: '/',
    element: <Home />,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/login',
    element: <Login />,
    meta: {
      title: '登录'
    }
  }
]

const routers: AuthRouteObject<MetaMenu>[] = [...BASE_ROUTERS, ...MENU_ROUTERS]
export default routers
